package com.lzxuni.modules.dataAcquisition;

import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.entity.ChainLink;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkService;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/LR_DataAcquisition/Traceability")
public class TraceabilityController extends BaseController {

    @Autowired
    private ChainService chainService;

    @Autowired
    private ChainLinkService chainLinkService;

    @RequestMapping("/Index")
    @SysLog(categoryId = 2, module = "追溯", operateType = "访问")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/Traceability/index");
        return mv;
    }

    @RequestMapping("/GetList")
    public Object GetList(String pagination, Chain chain, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        chain.setChainName(Keyword);
        PageData pageData = getPageData(chainService.queryPage(pageParameter, chain));
        return R.ok().put("data", pageData);
    }

    @RequestMapping("/SubIndex")
    public ModelAndView Sublist(String parentId, String type, String productId) {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/Traceability/subindex");
        mv.addObject("parentId", parentId);
        mv.addObject("type", type);
        mv.addObject("productId", productId);
        return mv;
    }

    @RequestMapping("/Preview")
    public ModelAndView Preview(String chainId) {
        ModelAndView mv = new ModelAndView("/LR_DataAcquisition/Traceability/preview1");
        mv.addObject("chainId", chainId);
        return mv;
    }

    @RequestMapping("/GetTree")
    public Object GetTree(String chainId) throws SQLException  {
        List<Map<String, Object>> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(chainId)) {
            ChainLink chainLink = new ChainLink();
            chainLink.setParentId(chainId);
            list = chainLinkService.getTree(chainLink);
        }
        return R.ok().put("data",list);
    }

    @RequestMapping("/GetTree1")
    public Object GetTree1(String chainId) throws SQLException  {
        if (StringUtils.isEmpty(chainId)) {
            chainId = "0";
        }
        List<Tree> list = chainLinkService.getTree(chainId);
        return R.ok().put("data",list);
    }

}
